<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Manager_Model extends CI_Model
{
	/*
	 *
	 * Store 
	 *
	 */

	function get_store($user_id){
		$this->db->where('user_id', $user_id);

		$query = $this->db->get('store');
		if ($query->num_rows() == 1) return $query->row();
		return NULL;

	}

	function update_store($user_id, $data){
		$this->db->where('user_id', $user_id);
		$this->db->update('store', $data);	
	}


	/*
	 *
	 * Products 
	 *
	 */

	function get_list_product($user_id='',$page='', $limit='',$where=''){

		$this->db->where('user_id', $user_id);

		if(!empty($limit))
		$this->db->limit($limit,$page);

		$query = $this->db->get('product');

		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}	

	function get_product($product_id){

		if (is_array($product_id)){

			$product = array();

			foreach ($product_id as $key => $val) {
				$this->db->where('product_id', $product_id);

				$query = $this->db->get('product');
				if ($query->num_rows() == 1) 
					$product[] = $query->row();
				
			}

			return $product;

		}
		else{
			$this->db->where('product_id', $product_id);

			$query = $this->db->get('product');
			if ($query->num_rows() == 1) return $query->row();
			return NULL;
		}

	}

	function save_product($data,$return=false){
		$tabelname = 'product';
		if ($this->db->insert($tabelname, $data)) {
			$field_id = $this->db->insert_id();
			if($return) return $field_id;
		}
		return TRUE;
	}

	function update_product($product_id,$data){
		$this->db->where('product_id', $product_id);
		$this->db->update('product', $data);	
	}

	function delete_product($product_id,$user_id=''){
		$this->db->where('product_id', $product_id);

		if (!empty($user_id)){
			$this->db->where('user_id', $user_id);
		}
		
		$this->db->delete('product');
		
		return TRUE;
	}

	function save_product_option($option, $return=false){
		$tabelname = 'product_option';
		if ($this->db->insert($tabelname, $option)) {
			$field_id = $this->db->insert_id();
			if($return) return $field_id;
		}
		return TRUE;

	}

	function update_product_option($product_option_id, $data){
		$this->db->where('product_option_id', $product_option_id);
		$this->db->update('product_option', $data);	
	}

	function get_product_option($product_id){
		$this->db->where('product_id', $product_id);
		$this->db->group_by('option_name');

		$query = $this->db->get('product_option');
		if ($query->num_rows() > 0){

			foreach ($query->result() as $key => $val) {

				$product_option[$key]['data']['option_name'] = $val->option_name;

				$option_name = $val->option_name;

				$q_op = $this->db->query("select * from product_option where option_name ='$option_name' and product_id='$product_id' ");

				$product_option[$key]['option'] = $q_op->result();
			}

			return $product_option; 

			
		} 
		return array();
	}
	
	function delete_product_option($product_option_id,$product_id){
		$this->db->where('product_option_id', $product_option_id);
		$this->db->where('product_id', $product_id);
		
		$this->db->delete('product_option');
		
		return TRUE;
	}


	function get_category(){
		$this->db->where('parent', 0);

		$query = $this->db->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return NULL;
	}

	function get_category_child($parent){
		$this->db->where('parent', $parent);

		$query = $this->db->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}

	function get_product_weight_class(){
		$query = $this->db->get('product_weight_class');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}

	function get_product_length_class(){
		$query = $this->db->get('product_length_class');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}

	/*
	 *
	 * Shipping
	 *
	 */

	function get_product_shipping($product_id){
		$this->db->where('product_id', $product_id);

		$query = $this->db->get('product_shipping');
		if ($query->num_rows() > 0) return $query->result_array();
		return array();

	}

	function save_product_shipping($data,$return=false){
		$tabelname = 'product_shipping';
		if ($this->db->insert($tabelname, $data)) {
			$field_id = $this->db->insert_id();
			if($return) return $field_id;
		}
		return TRUE;
	}

	function update_product_shipping($product_id,$data){
		$this->db->where('product_id', $product_id);
		$this->db->update('product_shipping', $data);	
	}

	function delete_product_shipping($shipping_id,$product_id){
		$this->db->where('shipping_id', $shipping_id);
		$this->db->where('product_id', $product_id);
		
		$this->db->delete('product_shipping');
		
		return TRUE;
	}

	function get_shipping_area($city){
		$this->db->like('name', $city);
		$this->db->order_by('name');
		$this->db->limit(7);
		$query = $this->db->get('city');
		
		if ($query->num_rows() > 0) return $query->result_array();
		return array();
	}
	
	function getTotalSales($toko_id){
		return $this->db->query('select sum(total) as total_sales from `order` where toko_id = "'.$toko_id.'"')->row();	
	}
	
	function getTotalSalesByYear($year, $toko_id){
		return $this->db->query('select sum(`total`) as total_sales_year from `order` where YEAR(date_added) = "'.$year.'" and toko_id = "'.$toko_id.'"')->row();	
	}
	
	function getTotalSalesPaid($toko_id){
		return $this->db->query('select sum(total) as total_sales from `order` where payment_status_id = 2 and toko_id = "'.$toko_id.'"')->row();	
	}
	
	function getTotalSalesPaidByYear($year, $toko_id){
		return $this->db->query('select sum(`total`) as total_sales_year from `order` where YEAR(date_added) = "'.$year.'" and payment_status_id = 2 and toko_id = "'.$toko_id.'"')->row();	
	}
	
	function get_order_by_store($store_id){
		
		
		$query = $this->db->query('SELECT * FROM (`order`) WHERE order_id in (select order_id from order_product where store_id = "'.$store_id.'") order by date_added desc limit 10');
		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();	
	}
	
	function get_order_status(){
		$query = $this->db->get('order_status');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}

}
